
#include "CTCPServer.h"

ConnectRun   g_ConnectRun = NULL;   // ½ÓÊÕµ½ÐÅÏ¢µÄ»Øµ÷º¯Êý


/********************************
º¯  Êý£ºrun
¹¦  ÄÜ£º´¦ÀíÁ´½ÓÇëÇóµÄÖ÷º¯Êý
²Î  Êý£º

·µ»ØÖµ£º
Ëµ  Ã÷£º
********************************/
void CZhiTcpServerConnection::run()
{
	try
	{
		StreamSocket my_socket =  socket();              // »ñÈ¡socket

		if(g_ConnectRun != NULL)
		{
			g_ConnectRun(my_socket);
		}
	}
	catch( std::exception& e )
	{
		LOG(ERROR) << e.what();
	}
}

/********************************
º¯  Êý£ºCTCPServer
¹¦  ÄÜ£º
²Î  Êý£º

·µ»ØÖµ£º
Ëµ  Ã÷£º
********************************/
CTCPServer::CTCPServer(unsigned short port)
{
	m_ServerSocket = new Poco::Net::ServerSocket(port);

	m_Param        = new Poco::Net::TCPServerParams;
    m_Param->setMaxQueued( 120 );         //Éè¶¨¶ÓÁÐµÄ×î´ó³¤¶È¡£
    m_Param->setMaxThreads( 120 );        //Éè¶¨¡¡TcpServer ×î´óÏß³ÌµÄ¸öÊý¡£

	m_server       = new TCPServer( new CZhiTcpServer, *m_ServerSocket,m_Param );

    m_setSendTimeout(1000);               //ÉèÖÃ·¢ËÍ³¬Ê±

    mkdir("./Log",S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO);
    mkdir("./Log/loginfo",S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO);
    mkdir("./Log/logwarn",S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO);
    mkdir("./Log/logerr",S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO);

	google::InitGoogleLogging("ServerLog"); // ÉèÖÃÈÕÖ¾ÎÄ¼þÃûÖÐµÄ"ÎÄ¼þÃû"×Ö¶Î.

	google::SetLogDestination(google::GLOG_INFO,"./Log/loginfo/");
	google::SetLogDestination(google::GLOG_WARNING,"./Log/logwarn/");
	google::SetLogDestination(google::GLOG_ERROR,"./Log/logerr/");
	FLAGS_logtostderr = false;

	LOG(INFO) << "Server Start";           //ÈÕÖ¾

}
/********************************
º¯  Êý£º~CTCPServer
¹¦  ÄÜ£º
²Î  Êý£º

·µ»ØÖµ£º
Ëµ  Ã÷£º
********************************/
CTCPServer::~CTCPServer()
{
	m_server->stop();
    google::ShutdownGoogleLogging();     // Í£Ö¹GLog
	delete(m_server);
	delete(m_ServerSocket);
}
/********************************
º¯  Êý£ºm_setReceiveTimeout
¹¦  ÄÜ£º
²Î  Êý£º

·µ»ØÖµ£º
Ëµ  Ã÷£º
********************************/
void CTCPServer::m_setReceiveTimeout(long n)
{
	Poco::Timespan timeout(n ,0);
	m_ServerSocket->setReceiveTimeout(timeout);
}
/********************************
º¯  Êý£ºm_setSendTimeout
¹¦  ÄÜ£º
²Î  Êý£º

·µ»ØÖµ£º
Ëµ  Ã÷£º
********************************/
void CTCPServer::m_setSendTimeout(long n)
{
	Poco::Timespan timeout(n ,0);
	m_ServerSocket->setSendTimeout(timeout);
}
/********************************
º¯  Êý£ºm_setMessageSolve
¹¦  ÄÜ£º
²Î  Êý£º

·µ»ØÖµ£º
Ëµ  Ã÷£º
********************************/
void CTCPServer::m_setMessageSolve(ConnectRun v_mslove)
{
	g_ConnectRun = v_mslove;
}
/********************************
º¯  Êý£º
¹¦  ÄÜ£º·µ»Øµ±Ç°Ê¹ÓÃµÄÁ¬½ÓÏß³ÌµÄÊýÁ¿
²Î  Êý£º

·µ»ØÖµ£º
Ëµ  Ã÷£º
********************************/
int CTCPServer::m_getCurrentThreads()
{
  return m_server->currentThreads();
}
/********************************
º¯  Êý£º
¹¦  ÄÜ£º·µ»Ø¿ÉÓÃÏß³ÌµÄ×î´óÊýÁ¿
²Î  Êý£º

·µ»ØÖµ£º
Ëµ  Ã÷£º
********************************/
int CTCPServer::m_getMaxThreads()
{
  return m_server->maxThreads();
}
/********************************
º¯  Êý£º
¹¦  ÄÜ£º·µ»Ø´¦ÀíÁ¬½ÓµÄ×ÜÊý
²Î  Êý£º

·µ»ØÖµ£º
Ëµ  Ã÷£º
********************************/
int CTCPServer::m_getTotalConnections()
{
  return m_server->totalConnections();
}
/********************************
º¯  Êý£º
¹¦  ÄÜ£º·µ»Øµ±Ç°´¦ÀíµÄÁ¬½ÓÊý
²Î  Êý£º

·µ»ØÖµ£º
Ëµ  Ã÷£º
********************************/
int CTCPServer::m_getCurrentConnections()
{
  return m_server->currentConnections();
}
/********************************
º¯  Êý£º
¹¦  ÄÜ£º·µ»Ø²¢·¢´¦ÀíÁ¬½ÓµÄ×î´óÊýÄ¿
²Î  Êý£º

·µ»ØÖµ£º
Ëµ  Ã÷£º
********************************/
int CTCPServer::m_getMaxConcurrentConnections()
{
  return m_server->maxConcurrentConnections();
}
/********************************
º¯  Êý£º
¹¦  ÄÜ£º·µ»Ø¶ÓÁÐÁ¬½ÓµÄÊýÁ¿
²Î  Êý£º

·µ»ØÖµ£º
Ëµ  Ã÷£º
********************************/
int CTCPServer::m_getQueuedConnections()
{
  return m_server->queuedConnections();
}
/********************************
º¯  Êý£º
¹¦  ÄÜ£º·µ»Ø¾Ü¾øÁ¬½ÓµÄÊýÄ¿
²Î  Êý£º

·µ»ØÖµ£º
Ëµ  Ã÷£º
********************************/
int CTCPServer::m_getRefusedConnections()
{
  return m_server->refusedConnections();
}
